package com.tokenplus.thai.receive

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.R
import com.tokenplus.thai.aviewmodel.UserViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.databinding.ActivityReceiveBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.*
import com.tokenplus.thai.home.repo.HomeCoinManager
import com.tokenplus.thai.utils.QRCodeUtil

/** receive页 */
@SuppressLint("CustomSplashScreen")
class ReceiveActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityReceiveBinding

    //user viewModel
    private val mUserViewModel by lazy {
        ViewModelProvider(this)[UserViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityReceiveBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化
            mActivityBinding.textReceiveWalletAddress.text =
                HomeCoinManager.mGlobalAddressLiveData.value

            //计算qr code宽高
            calculateQRCodeSize()

            //监听返回
            onHandleLiveDataCallback()

            //更新UI
            mActivityBinding.textReceiveTitle.setText(
                getString(
                    R.string.receive_support_tips,
                    HomeCoinManager.mGlobalSymbolLiveData.value,
                    HomeCoinManager.mGlobalNameLiveData.value
                )
            )

            //点击复制
            mActivityBinding.llReceiveCopy.setOnClickListener {
                val text = mActivityBinding.textReceiveWalletAddress.text.toString().trim()
                copyToClipboard(text)
            }

            //点击分享
            mActivityBinding.llReceiveShare.setOnClickListener {
                val body = getString(R.string.app_name)//getString(R.string.invite_share_content)
                val link = mActivityBinding.textReceiveWalletAddress.text.toString()
                    .trim()//"https://www.tokenplusapp.com?referre=GHU654"
                val content = body + "\n\n" + link
                openSysShareTextIntent(content)
            }

            //生成qr code
            Handler(Looper.getMainLooper()).postDelayed({
                createQRCode(HomeCoinManager.mGlobalAddressLiveData.value.toString())
            }, 1000)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mUserViewModel.onGetMyInviteInfoLiveData.observe(this) {
//            if (it.isSuccess) {
//                it.getOrNull()?.let { result ->
//                    val refer = result.inviteCode
//                    val rate = result.incomeRate.toFinancePercentString()
//                }
//            }
        }
    }

    /** 计算qr code宽高 */
    private fun calculateQRCodeSize() {
        val parentPadding = mActivityBinding.rlReceiveQRCode.dip2Px(32F)
        val parentViewWidth = getScreenWidth() - parentPadding

        val parentViewParams = mActivityBinding.rlReceiveQRCode.layoutParams
        parentViewParams.height = parentViewWidth
        mActivityBinding.rlReceiveQRCode.layoutParams = parentViewParams

        mActivityBinding.viewReceiveQRCodeBorder.invalidate()
    }

    /** 生成qr code */
    private fun createQRCode(content: String) {
        val barCodeBitmap = QRCodeUtil.generateQRCode(
            mActivityBinding.imageReceiveQRCode.width,
            mActivityBinding.imageReceiveQRCode.height,
            content
        )
        mActivityBinding.imageReceiveQRCode.setImageBitmap(barCodeBitmap)

    }
}